<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>findLastIndex</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const findLastIndex = (arr, handler) =>
            (arr
                .map((val, i) => [i, val])
                .filter(([i, val]) => handler(val, i, arr))
                .pop() || [-1])[0];
        console.log(findLastIndex([1, 2, 3, 4], n => n % 2 === 1)); // 2 (index of the value 3)
        console.log(findLastIndex([1, 2, 3, 4], n => n === 5)); // -1 (default value when not found)
    </script>
</body>

</html>